<template>
	<view>
		<image src="../../static/login/top.jpg" mode="widthFix" class="top"></image>
		<image src="../../static/login/logo.png" mode="widthFix" class="logo"></image>
		<view class="desc">
			<text class="name">（乘客端）</text>
			<!-- <text class="version">Ver1.0.0</text> -->
		</view>
		<!--  @tap="login" -->
		<button class="btn" open-type="getPhoneNumber" @getphonenumber="login">微信登陆</button>
		<!--  <view class="register-container">
      没有账号?
      <text class="link" @tap="register">立即注册</text>
    </view> -->
		<text class="remark">本小程序仅限于嘿代驾客户使用，用户必须先进行登录操作方可在本小程序中下单。</text>
		<u-toast ref="uToast" />
	</view>
</template>

<script>
export default {
	data() {
		return {
			code: null
		};
	},
	methods: {
		register: function() {
			let that = this;
			uni.showModal({
				title: '提示信息',
				editable: true,
				placeholderText: '输入手机号',
				success: function(resp) {
					let tel = resp.content;
					if (resp.confirm) {
						let bool = that.checkValidTel(tel, '手机号码');
						if (bool) {
							uni.login({
								provider: 'weixin',
								success: function(resp) {
									let code = resp.code;
									that.code = code;
								}
							});

							uni.getUserProfile({
								desc: '获取用户信息',
								success: function(resp) {
									let nickname = resp.userInfo.nickName;
									let avatarUrl = resp.userInfo.avatarUrl;
									let sex = resp.userInfo.gender;
									let sexJson = {
										0: '无',
										1: '男',
										2: '女'
									};
									sex = sexJson[sex + ''];
									let data = {
										code: that.code,
										nickname: nickname,
										photo: avatarUrl,
										sex: sex,
										tel: tel
									};
									that.ajax(that.url.registerNewCustomer, 'POST', data, function(resp) {
										let token = resp.data.token;
										uni.setStorageSync('token', token);
										that.$refs.uToast.show({
											title: '注册成功',
											type: 'success',
											callback: function() {
												uni.switchTab({
													url: '../workbench/workbench'
												});
											}
										});
									});
								}
							});
						}
					}
				}
			});
		},

		//登录
		async login(e) {
			let phoneCode = e.detail.code;

			//获取微信登录的code
			let wxLoginCode = await this.getWxLoginCode();
			// console.log('获取到的登录code是:',wxLoginCode)

			//获取用户信息
			let { nickname, avatarUrl, sex } = await this.getUserInfo();
			// console.log(`获取到的用户信息是:${nickname}----${avatarUrl}`);
			uni.setStorageSync('userName', nickname);

			this.doLoginAction({
				code: wxLoginCode,
				phoneCode: phoneCode,
				nickname: nickname,
				photo: avatarUrl,
				sex: sex
			});
		},

		//获取微信登录信息
		getWxLoginCode() {
			return new Promise((resolve, reject) => {
				uni.login({
					success(resp) {
						let code = resp.code;
						this.code = code;
						resolve(code);
					}
				});
			});
		},

		//获取登录的用户信息
		getUserInfo() {
			return new Promise((resolve, reject) => {
				uni.getUserInfo({
					provider: 'weixin',
					success: resp => {
						// console.log('获取的用户信息是:',resp)
						let nickname = resp.userInfo.nickName;
						let avatarUrl = resp.userInfo.avatarUrl;
						let sex = resp.userInfo.gender;
						let sexJson = {
							0: '无',
							1: '男',
							2: '女'
						};
						sex = sexJson[sex + ''];

						resolve({ nickname, avatarUrl, sex });
					}
				});
			});
		},

		doLoginAction(param) {
			let that = this;
			that.ajax(that.url.login, 'POST', param, function(resp) {
				if (!resp.data.hasOwnProperty('token')) {
					that.$refs.uToast.show({
						title: '请先注册',
						type: 'error'
					});
				} else {
					let token = resp.data.token;
					uni.setStorageSync('token', token);
					that.$refs.uToast.show({
						title: '登录成功',
						type: 'success',
						callback: function() {
							uni.switchTab({
								url: '../workbench/workbench'
							});
						}
					});
				}
			});
		}
	},
	
	onLoad() {
		//如果用户登录过,则直接进入workbench界面
		let token = uni.getStorageSync('token');
		// console.log(`token的值是${token}`);
		if (token) {
			uni.switchTab({
				url: '../workbench/workbench'
			});
		}
	}
};
</script>

<style lang="less">
@import url('login.less');
</style>
